-----------------------------------------------------------------------
-- 本文件为脚本入口文件，请不要删除
-- 该脚本适用于脚本精灵2.0.1及以上版本
-- 脚本从main()开始执行
-- 脚本界面从createUI()开始执行，脚本可以无界面
-----------------------------------------------------------------------

-- 创建脚本界面入口
function createUI()
	showLoopSetting(false)
	
	createTextView("main_level_text","---選擇關卡---------")
	newLine()
	createButton("main_level_btn","主線","main_level_function")
	createTextView("main_level_text2"," ")
	createButton("tower_level_btn","塔","tower_level_function")
	createTextView("text2223"," ")
	createButton("travellers_btn","旅人","travellers_function")
	newLine()
	createTextView("teammate","---選擇隊友---------")
	newLine()
	createCheckBox("teammateFlag","啟動",false)
	newLine()
	createRadioBox("teammateType","屬性",true)
	createRadioBox("teammateType","生命力",false)
	createRadioBox("teammateType","攻擊力",false)
	createRadioBox("teammateType","回復力",false)
	newLine()
	createRadioBox("teammateType","空間",false)
	createRadioBox("teammateType","稀有度",false)
	createRadioBox("teammateType","已選次數",false)
	newLine()
	createRadioBox("teammateAttribute","水",true)
	createRadioBox("teammateAttribute","火",false)
	createRadioBox("teammateAttribute","木",false)
	createRadioBox("teammateAttribute","光",false)
	createRadioBox("teammateAttribute","暗",false)
	newLine()
	createRadioBox("teammateRace","神",true)
	createRadioBox("teammateRace","魔",false)
	createRadioBox("teammateRace","人",false)
	createRadioBox("teammateRace","獸",false)
	createRadioBox("teammateRace","龍",false)
	createRadioBox("teammateRace","妖",false)
	newLine()
	
end

function main_level_function()
	createWindow("main_level_window")
	createTextViewEx("main_level_window","level_choice","主線")
	newLine("main_level_window")
	createRadioBoxEx("main_level_window","main_level_1","水",true)
	createRadioBoxEx("main_level_window","main_level_1","火",false)
	createRadioBoxEx("main_level_window","main_level_1","木",false)
	createRadioBoxEx("main_level_window","main_level_1","光",false)
	createRadioBoxEx("main_level_window","main_level_1","暗",false)
	newLine("main_level_window")
	createRadioBoxEx("main_level_window","main_level_2","1-3",true)
	createRadioBoxEx("main_level_window","main_level_2","2-5",false)
	createRadioBoxEx("main_level_window","main_level_2","4-5",false)
	createRadioBoxEx("main_level_window","main_level_2","5-5",false)
	createRadioBoxEx("main_level_window","main_level_2","6-2",false)
	newLine("main_level_window")
	createRadioBoxEx("main_level_window","main_level_2","6-4",false)
	createRadioBoxEx("main_level_window","main_level_2","7-1",false)
	newLine("main_level_window")
	
end

function tower_level_function()
	createWindow("tower_level_window")
	createTextViewEx("tower_level_window","level_choice","塔")
	newLine("tower_level_window")
	createRadioBoxEx("tower_level_window","tower_level","4-5",true)
	
end

function travellers_function()
	createWindow("travellers_window")
	createTextViewEx("travellers_window","text2224","旅人")
	newLine("travellers_window")
	createButtonEx("travellers_window","constellation1_btn","星座上","constellation1_function")
	createButtonEx("travellers_window","constellation2_btn","星座下","constellation2_function")
	createButtonEx("travellers_window","gods_btn","封神","gods_function")
	newLine("travellers_window")
	
end

function constellation1_function()
	createWindow("constellation1_window")
	createTextViewEx("constellation1_window","level_choice","旅人-星座上")
	newLine("constellation1_window")
	createRadioBoxEx("constellation1_window","constellation1","白羊",true)
	createRadioBoxEx("constellation1_window","constellation1","金牛",false)
	createRadioBoxEx("constellation1_window","constellation1","雙子",false)
	createRadioBoxEx("constellation1_window","constellation1","巨蟹",false)
	newLine("constellation1_window")
	createRadioBoxEx("constellation1_window","constellation1","獅子",false)
	createRadioBoxEx("constellation1_window","constellation1","處女",false)
	newLine("constellation1_window")
	createRadioBoxEx("constellation1_window","constellation_level","高級",true)
	createRadioBoxEx("constellation1_window","constellation_level","中級",false)
	createRadioBoxEx("constellation1_window","constellation_level","初級",false)
end

function constellation2_function()
	createWindow("constellation2_window")
	createTextViewEx("constellation2_window","level_choice","旅人-星座下")
	newLine("constellation2_window")
	createRadioBoxEx("constellation2_window","constellation2","天秤",true)
	createRadioBoxEx("constellation2_window","constellation2","天蠍",false)
	createRadioBoxEx("constellation2_window","constellation2","人馬",false)
	createRadioBoxEx("constellation2_window","constellation2","山羊",false)
	newLine("constellation2_window")
	createRadioBoxEx("constellation2_window","constellation2","水瓶",false)
	createRadioBoxEx("constellation2_window","constellation2","雙魚",false)
	newLine("constellation2_window")
	createRadioBoxEx("constellation2_window","constellation_level","高級",true)
	createRadioBoxEx("constellation2_window","constellation_level","中級",false)
	createRadioBoxEx("constellation2_window","constellation_level","初級",false)
end

function gods_function()
	createWindow("gods_window")
	createTextViewEx("gods_window","level_choice","旅人-封神")
	newLine("gods_window")
	createRadioBoxEx("gods_window","gods","木",true)
	createRadioBoxEx("gods_window","gods","水",false)
	createRadioBoxEx("gods_window","gods","光",false)
	createRadioBoxEx("gods_window","gods","暗",false)
	createRadioBoxEx("gods_window","gods","火",false)
	newLine("gods_window")
	createRadioBoxEx("gods_window","gods_level","高級",true)
	createRadioBoxEx("gods_window","gods_level","中級",false)
	createRadioBoxEx("gods_window","gods_level","初級",false)
end

function getSuitPoint(x,y)
	Rx,Ry = getResolutionRatio()
	return Rx/720*x,Ry/1280*y
end

function preSetting()
	log("==========================================================")
	Rx,Ry = getResolutionRatio()
	log(string.format("螢幕解析度 %dx%d",Rx,Ry))
	log(string.format("選擇 %s%s,tower_level=%s,level_choice=%s",main_level_1,main_level_2,tower_level,level_choice))
	log("==========================================================")
	if main_level_1 == "水" then
		main_level_1_touch_x = 95
		main_level_1_touch_y = 522
	elseif main_level_1 == "火" then
		main_level_1_touch_x = 593
		main_level_1_touch_y = 489
	elseif main_level_1 == "木" then
		main_level_1_touch_x = 588
		main_level_1_touch_y = 913
	elseif main_level_1 == "光" then
		main_level_1_touch_x = 104
		main_level_1_touch_y = 823
	elseif main_level_1 == "暗" then
		main_level_1_touch_x = 643
		main_level_1_touch_y = 721
	end
	
	if main_level_2 == "1-3" then
		main_level_2_touch_x = 381
		main_level_2_touch_y = 886
		main_level_2_color_v = 12958307
		main_level_2_touch2_x = 548
		main_level_2_touch2_y = 408
	elseif main_level_2 == "2-5" then
		main_level_2_touch_x = 357
		main_level_2_touch_y = 742
		main_level_2_color_v = 2695193
		main_level_2_touch2_x = 368
		main_level_2_touch2_y = 337
	elseif main_level_2 == "4-5" then
		main_level_2_touch_x = 466
		main_level_2_touch_y = 442
		main_level_2_color_v = 2695193
		main_level_2_touch2_x = 368
		main_level_2_touch2_y = 337
	elseif main_level_2 == "5-5" then
		main_level_2_touch_x = 464
		main_level_2_touch_y = 338
		main_level_2_color_v = 2695193
		main_level_2_touch2_x = 368
		main_level_2_touch2_y = 337
	elseif main_level_2 == "6-2" then
		main_level_2_touch_x = 440
		main_level_2_touch_y = 547
		main_level_2_color_v = 2695193
		main_level_2_touch2_x = 403
		main_level_2_touch2_y = 730
	elseif main_level_2 == "6-4" then
		main_level_2_touch_x = 440
		main_level_2_touch_y = 547
		main_level_2_color_v = 2695193
		main_level_2_touch2_x = 461
		main_level_2_touch2_y = 439
	elseif main_level_2 == "7-1" then
		main_level_2_touch_x = 448
		main_level_2_touch_y = 400
		main_level_2_color_v = 12958307
		main_level_2_touch2_x = 437
		main_level_2_touch2_y = 673
	end
	
	if tower_level == "4-5" then
			
	end
end

-- 脚本执行入口
function main()
	local color,color2,color3
	
	preSetting()

	while true do
		sleep(3000)
		if level_choice == "主線" then
			main_level_proc()
		elseif level_choice == "塔" then
			tower_level_proc()
		elseif string.find(level_choice,"旅人") then
			travellers_proc()		
		end

	end --while true then
end

function travellers_proc()
--回到首頁
	touchClick(getSuitPoint(353,1089))
	sleep(3000)
--選取旅人
	while true do
		color = getColor(getSuitPoint(300,1009))
		log(string.format("選取旅人 color=%d",color))
		if color == 4868401 then
			log(string.format("click %d %d",getSuitPoint(602,313)))
			touchClick(getSuitPoint(602,313)) 
			break
		end
		sleep(1000)
	end

--滑動定位
	while true do
		color = getColor(getSuitPoint(56,249))
		log(string.format("滑動定位 color=%d",color))
		if color == 31157 then
			--找星座下
			while true do
				color = getColor(getSuitPoint(513,653))
				log(string.format("找星座下 color=%d",color))
				if color ~= 16777215 then
					myMoveLeft()
				else
					break
				end
				sleep(1500)
			end
			break	
		end
		
		sleep(1000)
	end

--選取星座/封神/...
	sleep(2000)
	log(string.format("選取星座/封神/..."))
	if string.find(level_choice,"星座") then
		if string.find(level_choice,"星座上") then
			touchClick(getSuitPoint(33,648))
		elseif string.find(level_choice,"星座下") then
			touchClick(getSuitPoint(375,654)) 		
		end
		sleep(2000)
		myMoveUp()
		--選取星座
		while true do
			color = getColor(getSuitPoint(656,925))
			log(string.format("選取星座 color=%d",color))
			if color == 12955235 then
				--選取各宮
				log(string.format("選取星座 level_choice=%s,constellation1=%s,constellation2=%s",level_choice,constellation1,constellation2))
				if string.find(level_choice,"星座上") then
					if constellation1 == "白羊" then
						touchClick(getSuitPoint(342,869)) 
					elseif constellation1 == "金牛" then
						touchClick(getSuitPoint(333,739)) 
					elseif constellation1 == "雙子" then
						touchClick(getSuitPoint(332,599)) 
					elseif constellation1 == "巨蟹" then
						touchClick(getSuitPoint(331,463)) 
					elseif constellation1 == "獅子" then
						touchClick(getSuitPoint(320,337)) 
					elseif constellation1 == "處女" then
						myMoveDown()
						sleep(5000)
						touchClick(getSuitPoint(324,420)) 
					end	
				elseif string.find(level_choice,"星座下") then
					if constellation2 == "天秤" then
						touchClick(getSuitPoint(342,869)) 
					elseif constellation2 == "天蠍" then
						touchClick(getSuitPoint(333,739)) 
					elseif constellation2 == "人馬" then
						touchClick(getSuitPoint(332,599)) 
					elseif constellation2 == "山羊" then
						touchClick(getSuitPoint(331,463)) 
					elseif constellation2 == "水平" then
						touchClick(getSuitPoint(320,337)) 
					elseif constellation2 == "雙魚" then
						myMoveDown()
						sleep(5000)
						touchClick(getSuitPoint(324,420)) 
					end	
				end
				
				--選取等級
				sleep(5000)
				if constellation_level == "高級" then
					touchClick(getSuitPoint(416,572))
				elseif constellation_level == "中級" then
					touchClick(getSuitPoint(406,725)) 						
				elseif constellation_level == "初級" then
					touchClick(getSuitPoint(407,859)) 						
				end
				break
			end	
			sleep(1000)
		end
	elseif string.find(level_choice,"封神") then
		myMoveRight()
		sleep(2000)
		while true do
			color = getColor(getSuitPoint(57,384))
			if color == 2163712 then
				touchClick(getSuitPoint(20,649)) 
				break			
			end	
			sleep(1000)	
		end
		sleep(2000)
		myMoveUp()
		while true do
			color = getColor(getSuitPoint(657,924))
			if color == 12428890 then
				if gods == "木" then
					touchClick(getSuitPoint(357,867))
				elseif gods == "水" then
					touchClick(getSuitPoint(358,742))	
				elseif gods == "光" then
					touchClick(getSuitPoint(344,597))	
				elseif gods == "暗" then
					touchClick(getSuitPoint(356,452))	
				elseif gods == "火" then
					touchClick(getSuitPoint(363,334))	
				end
				break		
			end	
			sleep(1000)	
		end
		
		sleep(5000)
		if gods_level == "高級" then
			touchClick(getSuitPoint(409,578))
		elseif gods_level == "中級" then
			touchClick(getSuitPoint(402,712)) 						
		elseif gods_level == "初級" then
			touchClick(getSuitPoint(392,854)) 						
		end
	end
	
	teammate_proc()
end

function main_level_proc()
--選取主線
	while true do
		color = getColor(getSuitPoint(300,1009))
		log(string.format("選取主線(%s) color=%d",main_level_1,color))
		if color == 4868401 then
			log(string.format("click %d %d",getSuitPoint(main_level_1_touch_x,main_level_1_touch_y)))
			touchClick(getSuitPoint(main_level_1_touch_x,main_level_1_touch_y)) 
			break
		end
		sleep(1000)
	end
--選取關卡
	--滑動
	while true do
		color = getColor(getSuitPoint(55,253))
		log(string.format("選取關卡(%s) color=%d",main_level_2.sub(main_level_2,0,1),color))
		if color == 556469 then
			if main_level_2 == "6-2" or main_level_2 == "6-4" or main_level_2 == "7-1" then
				myMoveDown()
			else
				myMoveUp()
			end
			
			break
		end
		sleep(1000)
	end
	
	--按下n-
	sleep(2000)
	while true do
		color = getColor(getSuitPoint(657,930))
		log(string.format("選取關卡(%s) color=%d",main_level_2.sub(main_level_2,0,1),color))
		if main_level_2 == "6-2" or main_level_2 == "6-4" or main_level_2 == "7-1" then
			if color == 2695184 then
				log(string.format("click %d %d",getSuitPoint(main_level_2_touch_x,main_level_2_touch_y)))
				touchClick(getSuitPoint(main_level_2_touch_x,main_level_2_touch_y)) 
				break
			end
		else
			if color == 7033146 then
				log(string.format("click %d %d",getSuitPoint(main_level_2_touch_x,main_level_2_touch_y)))
				touchClick(getSuitPoint(main_level_2_touch_x,main_level_2_touch_y)) 
				break
			end
		end
		sleep(1000)
	end
	
	--按下n-n
	--往上滑動
	if main_level_2 ~= "1-3" then
		sleep(3000)
		myMoveUp()
		sleep(2000)
	end
	
	while true do
		color = getColor(getSuitPoint(657,460))
		log(string.format("%s%s color=%d",main_level_1,main_level_2,color))
		if color == main_level_2_color_v then
			log(string.format("click %d %d",getSuitPoint(main_level_2_touch2_x,main_level_2_touch2_y)))
			touchClick(getSuitPoint(main_level_2_touch2_x,main_level_2_touch2_y)) 
			break
		end
		sleep(1000)
	end
	
	teammate_proc()
end

function tower_level_proc()
--選取塔
	while true do
		color = getColor(getSuitPoint(300,1009))
		log(string.format("選取塔 color=%d",color))
		if color == 4868401 then
			log(string.format("click %d %d",getSuitPoint(367,642)))
			touchClick(getSuitPoint(367,642)) 
			break
		end
		sleep(1000)
	end
	
--選取關卡
	--滑動
	while true do
		color = getColor(getSuitPoint(55,253))
		log(string.format("選取關卡(%s) color=%d",tower_level.sub(tower_level,0,1),color))
		if color == 556469 then
			myMoveUp()
			break
		end
		sleep(1000)
	end
	
	--按下n-
	sleep(2000)
	while true do
		color = getColor(getSuitPoint(657,930))
		log(string.format("選取關卡(%s) color=%d",tower_level.sub(tower_level,2,3),color))
		if color == 7033146 then
			log(string.format("click %d %d",getSuitPoint(394,340)))
			touchClick(getSuitPoint(394,340)) 
			break
		end
		sleep(1000)
	end
	
	--滑動
	sleep(2000)
	while true do
		color = getColor(getSuitPoint(55,253))
		log(string.format("選取關卡(%s) color=%d",tower_level.sub(tower_level,0,1),color))
		if color == 556469 then
			myMoveUp()
			break
		end
		sleep(1000)
	end
	
	--按下n-n
	while true do
		color = getColor(getSuitPoint(657,460))
		log(string.format("%s color=%d",tower_level,color))
		if color == 2695193 then
			log(string.format("click %d %d",getSuitPoint(421,339)))
			touchClick(getSuitPoint(421,339)) 
			break
		end
		sleep(1000)
	end
	
	teammate_proc()
end

function teammate_proc()
	--選人
	--勾選選擇隊友
	if teammateFlag == true then
		sleep(2000)
		while true do
			color = getColor(getSuitPoint(640,259))
			log(string.format("勾選選擇隊友 color=%d",color))
			if color == 1067354 then
				log(string.format("出現屬性按鈕 click %d %d",getSuitPoint(640,259)))
				touchClick(getSuitPoint(640,259))
				sleep(1000)
				--選擇屬性
				if teammateAttribute == "水" then
					touchClick(getSuitPoint(53,509))
				elseif teammateAttribute == "火" then
					touchClick(getSuitPoint(138,504))
				elseif teammateAttribute == "木" then
					touchClick(getSuitPoint(230,512))
				elseif teammateAttribute == "光" then
					touchClick(getSuitPoint(324,510))
				elseif teammateAttribute == "暗" then
					touchClick(getSuitPoint(414,508))
				end
				sleep(1000)
				--選擇種族
				if teammateRace == "神" then
					touchClick(getSuitPoint(58,640))
				elseif teammateRace == "魔" then
					touchClick(getSuitPoint(143,646))
				elseif teammateRace == "人" then
					touchClick(getSuitPoint(230,642))			
				elseif teammateRace == "獸" then
					touchClick(getSuitPoint(327,640))			
				elseif teammateRace == "龍" then
					touchClick(getSuitPoint(410,646))		
				elseif teammateRace == "妖" then
					touchClick(getSuitPoint(54,720))
				end
				sleep(1000)
				--選擇type
				sleep(1000)
				if teammateType == "屬性" then
					touchClick(getSuitPoint(576,367))
				elseif teammateType == "生命力" then
					touchClick(getSuitPoint(562,440))			
				elseif teammateType == "攻擊力" then
					touchClick(getSuitPoint(570,520))		
				elseif teammateType == "回復力" then
					touchClick(getSuitPoint(565,588))			
				elseif teammateType == "空間" then
					touchClick(getSuitPoint(567,659))		
				elseif teammateType == "稀有度" then
					touchClick(getSuitPoint(562,739))				
				elseif teammateType == "已選次數" then
					touchClick(getSuitPoint(561,812))			
				end
				break
			end
			sleep(1000)
		end
	end

	while true do
		color = getColor(getSuitPoint(363,977))
		color2 = getColor(getSuitPoint(277,1147))
		color3 = getColor(getSuitPoint(440,1154))
		log(string.format("選人 color=%d,color2=%d,color3=%d",color,color2,color3))
		if color == 3812624 and color2 == 3219472 and color3 == 2166792 then
			if teammateFlag == true then
				log(string.format("click %d %d",getSuitPoint(352,951)))
				touchClick(getSuitPoint(352,951))
			else
				log(string.format("click %d %d",getSuitPoint(540,410)))
				touchClick(getSuitPoint(540,410))
			end
			break
		end
		sleep(1000)
	end

--確定選人
	while true do
		color = getColor(getSuitPoint(211,763))
		log(string.format("確定選人 color=%d",color))
		if color == 1051664 then
			log(string.format("click %d %d",getSuitPoint(419,736)))
			touchClick(getSuitPoint(419,736))
			break
		end
		sleep(1000)
	end

--進入戰鬥
	while true do
		color = getColor(getSuitPoint(543,1087))
		color2 = getColor(getSuitPoint(690,1144))
		log(string.format("進入戰鬥 color=%d,color2=%d",color,color2))
		if color == 529441 and color2 == 2056 then
			log(string.format("click %d %d",getSuitPoint(620,1123)))
			touchClick(getSuitPoint(620,1123))
			break
		end
		sleep(1000)
	end

--等待戰鬥結束
	sleep(10000)
	while true do
		color = getColor(getSuitPoint(67,686))
		log(string.format("等待戰鬥結束 color=%d",color))
		if color == 525312 then
			--log(string.format("click 540 410"))
			sleep(10000)
			for i=400,880,5 do
				--touchClick(360,i)
				touchDown(getSuitPoint(360,i))
				sleep(50)
				touchUp()
				log(string.format("等待戰鬥結束 click %d %d",getSuitPoint(360,i)))
			end
			break
		end
		sleep(1000)
	end

--有可能卡片升級
	sleep(3000)
	while true do
		color = getColor(getSuitPoint(300,1009))
		log(string.format("有可能卡片升級 color=%d",color))
		if color == 4868401 then
			break;		
		end
		touchClick(getSuitPoint(585,708))
		sleep(1000)
	end
	
--取消加入好友
	while true do
		color = getColor(getSuitPoint(300,1009))
		log(string.format("取消加入好友 color=%d",color))
		if color == 4868401 then
			log(string.format("click %d %d",getSuitPoint(512,822)))
			touchClick(getSuitPoint(512,822))
			sleep(50)
			if teammateFlag == true then
				for i=320,360,5 do
					touchClick(getSuitPoint(i,820))
					log(string.format("click %d %d",getSuitPoint(i,820)))
				end
			end
			break
		end
		sleep(1000)
	end
end

function myMoveUp()
	log(string.format("往上滑動"))
	touchDown(getSuitPoint(360,800))
	sleep(50)
	touchMove(getSuitPoint(360,200))	
	sleep(50)
	touchUp();
end

function myMoveLeft()
	log(string.format("往左滑動"))
	touchDown(getSuitPoint(375,654))
	sleep(50)
	touchMove(getSuitPoint(28,655))
	sleep(50)
	touchUp()
end

function myMoveRight()
	log(string.format("往右滑動"))
	touchDown(getSuitPoint(375,654))
	sleep(50)
	touchMove(getSuitPoint(725,655))
	sleep(50)
	touchUp()
end

function myMoveDown()
	log(string.format("往下滑動"))
	touchDown(getSuitPoint(360,460))
	sleep(50)
	for i=460,960,20 do
		touchMove(getSuitPoint(360,i))
		sleep(10)
		log(string.format("往下滑動 %d %d",getSuitPoint(360,i)))
	end
	touchUp()
end

--if getScriptElfVersion() < 201 then
--	main()
--end

